package zl.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import zl.bean.Admin;
import zl.bean.RespBean;
import zl.service.LoginService;
import zl.service.UserService;
import zl.utils.JsonUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/")
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private LoginService loginService;

    @ResponseBody
    @RequestMapping("/login")
    public RespBean login(String username , String password , String codeInput , String al , HttpSession session , HttpServletResponse response){
        String code = (String) session.getAttribute("code");
        if(code.equalsIgnoreCase(codeInput)){
            Admin admin = loginService.selectUser(username, password);
            if(admin!=null){
                if(al!=null&&al.equals("on")){
                    // 将用户信息存储到Cookie中，并设置过期时间为5天
                    Cookie cookie1 = new Cookie("userInfo1", username);
                    cookie1.setMaxAge(5 * 24 * 60 * 60); // 5天的秒数
                    response.addCookie(cookie1);
                    Cookie cookie2 = new Cookie("userInfo2", password);
                    cookie2.setMaxAge(5 * 24 * 60 * 60); // 5天的秒数
                    response.addCookie(cookie2);
                    //继续登录
                    session.setAttribute("admin", admin);
                        return RespBean.ok("登录成功~~");
                }else{
                    session.setAttribute("admin", admin);
                    return RespBean.ok("登录成功~~");
                }
            }else{
                return RespBean.error("用户名或密码错误");
            }
        }else{
            return RespBean.error("验证码错误");
        }

    }

    @ResponseBody
    @RequestMapping("/al")
    public RespBean al(String username , String password ,HttpSession session){
        Admin admin = loginService.selectUser(username, password);
        session.setAttribute("admin", admin);
        return RespBean.ok("登录成功~~");
    }

    @ResponseBody
    @RequestMapping("/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().invalidate();
        try {
            Cookie cookie1 = new Cookie("userInfo1", "");
            cookie1.setMaxAge(0);
            response.addCookie(cookie1);
            Cookie cookie2 = new Cookie("userInfo2", "");
            cookie2.setMaxAge(0);
            response.addCookie(cookie2);
            JsonUtil.toJSON(response.getOutputStream(), RespBean.ok("正在退出~~"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
